package cn.cs.service.impl;

import cn.cs.client.AgentSettlementDetailedClient;
import cn.cs.client.AgentUserClient;
import cn.cs.client.SysDtAreaClient;
import cn.cs.common.Dto;
import cn.cs.common.DtoUtil;
import cn.cs.common.EmptyUtils;
import cn.cs.common.RedisUtils;
import cn.cs.service.AgentSettlementService;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AgentSettlementServiceImpl implements AgentSettlementService {
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private AgentUserClient agentUserClient;
    @Resource
    private  AgentSettlementDetailedClient agentSettlementDetailedClient;
    @Resource
    private SysDtAreaClient sysDtAreaClient;
    @Override
    public Dto agentBalance(HttpServletRequest request) {
        String token = request.getHeader("token");
        Map<String,Object> agentUser =  JSON.parseObject(redisUtils.getString(token), Map.class);
        Map<String,Object> user = agentUserClient.getAgentUserByPhone(agentUser.get("phone").toString());
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("code",user.get("invitation_code").toString());
        param.put("level",user.get("level").toString());
//        int n = mapper.getBalance(param);
        List<Map<String,Object>> list = agentSettlementDetailedClient.getSetHisList(user.get("id").toString());
        Map<String,Object> retMap = new HashMap<String, Object>();
//        retMap.put("earning",n);
        retMap.put("list",list);
        return DtoUtil.returnDataSuccess(retMap);
    }

    @Override
    public Dto agentSeetHisList(HttpServletRequest request) {
        String token = request.getHeader("token");
        Map<String,Object> agentUser =  JSON.parseObject(redisUtils.getString(token), Map.class);
        if(EmptyUtils.isEmpty(agentUser)){
            return DtoUtil.returnFail("用户未登录","001");
        }
        List<Map<String,Object>> list = agentSettlementDetailedClient.getSetHisList(agentUser.get("id").toString());
        if(EmptyUtils.isNotEmpty(list)){
            return DtoUtil.returnDataSuccess(list);
        }else{
            return DtoUtil.returnFail("没有结算历史","002");
        }
    }

    @Override
    public Dto agentSeetInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        Map<String,Object> agentUser =  JSON.parseObject(redisUtils.getString(token), Map.class);
        String id = request.getParameter("id");
        Map<String,Object> setMap = agentSettlementDetailedClient.getSetInfoById(id);
        if(EmptyUtils.isNotEmpty(setMap)){
            float setMoney = Float.valueOf(setMap.get("set_money").toString());
            float captailMoney = Float.valueOf(setMap.get("capital_pool_money").toString());
            setMap.put("set_money",turnType(String.valueOf(setMoney)));
            setMap.put("capital_pool_money",turnType(String.valueOf(captailMoney)));
            float actual_money = setMoney - captailMoney;
            setMap.put("actual_money",turnType(String.valueOf(actual_money)));
            return DtoUtil.returnDataSuccess(setMap);
        }else{
            return DtoUtil.returnFail("没有结算历史","002");
        }
    }

    @Override
    public Dto capitalInfoList(HttpServletRequest request) throws Exception{
        List<Map<String,Object>> list = agentSettlementDetailedClient.capitalInfoList();
        for(Map<String,Object> map:list){
            String money = map.get("money").toString();
            map.put("money",money.replaceAll("-",""));
            map.put("area_name",getPCareaContent(map.get("areaId").toString()));
        }
        if(EmptyUtils.isNotEmpty(list)){
            return DtoUtil.returnDataSuccess(list);
        }else{
            return DtoUtil.returnFail("没有结算历史","002");
        }
    }

    public String turnType (String str){
        Double cny = Double.parseDouble(String.valueOf(str));//转换成Double
        DecimalFormat df = new DecimalFormat("0.00");//格式化
        String CNY = df.format(cny);
        return CNY;
    }

    public String getPCareaContent(String areaId) throws Exception{
        Map<String,Object> areaf = sysDtAreaClient.getSysDtAreaById(areaId);
        Map<String,Object> areas = sysDtAreaClient.getSysDtAreaById(areaf.get("area_parent_id").toString());
        Map<String,Object> areat = sysDtAreaClient.getSysDtAreaById(areas.get("area_parent_id").toString());
        return  areat.get("area_name")+" "+areas.get("area_name")+" "+areaf.get("area_name");
    }
}
